#include "Item.h"
#include <QDebug>

Item::Item(int id, QWidget *qWidget, QObject *object){
    this->id = id;
    this->qWidget = qWidget;
    this->desc = "label";
    this->img = "";
    this->setMouseTracking(true);
}

void Item::setDesc(QString desc){
    this->desc = desc;
    emit setDescSig(desc);
}

void Item::setImg(QString img){
    this->img = img;
}

void Item::getMouseEnterSlot(int id, bool isEnter){
    if(id == this->id){
        if(isEnter){
            //qDebug() << "getMouseEnterSig: mouse enter";
            emit setDescSig("mouse enter");
        } else{
            //qDebug() << "getMouseEnterSig: mouse out";
            emit setDescSig("mouse out");
        }
    } else{
        //qDebug() << "getMouseEnterSig: mouse out";
        emit setDescSig("mouse out");
    }
}

QPoint Item::getPosInMainWindow(QWidget *qWidget)
{
    QPoint posInMainWindow = qWidget->pos();

    QWidget* qParent = qWidget->parentWidget();
    int max = 10;
    while(QString::compare(qParent->objectName(), "MainWindow")){
        posInMainWindow = posInMainWindow + qParent->pos();
        //qDebug() << "parentWidget: " << parentWidget->objectName() << "   posInMainWindow: " << posInMainWindow;
        qParent = qParent->parentWidget();

        if(max > 0){
            max--;
        }else{
            break;
        }
    }

    return posInMainWindow;
}

void Item::showDetail(QWidget *qWidget)
{
    QWidget *mainWindowsWidget = findMainWindowsWidget(this->qWidget);
    QLabel *qDetail = mainWindowsWidget->findChild<QLabel *>(qWidget->objectName() + "_detailLabel");
    if(qDetail != nullptr){
        qDebug() << "find " << qDetail->objectName();
        qDetail->setVisible(true);
    } else {
        QLabel *qLabelDetail = new QLabel("detailLabel", mainWindowsWidget);
        QPoint qWidgetPos = getPosInMainWindow(qWidget);
        qLabelDetail->setText(qWidget->objectName() + ":detail");
        qLabelDetail->setParent(mainWindowsWidget);
        qLabelDetail->setObjectName(qWidget->objectName() + "_detailLabel");
        qLabelDetail->setGeometry(qWidgetPos.x() + qWidget->width(), qWidgetPos.y(), qWidget->width(), qWidget->height() * 2);
        qLabelDetail->setStyleSheet("QWidget{border:1px; "  //设置边框宽度
                                "border-style:solid;" //设置边框风格
                       "border-color:green;"  //设置边框颜色
                                "background: white;}"); //设置widget背景色
        qLabelDetail->setVisible(true);
        qLabelDetail->show();
        qDebug() << "new " << qLabelDetail->objectName();
    }
}

void Item::disableDetail(QWidget *qWidget)
{
    QWidget *mainWindowsWidget = findMainWindowsWidget(this->qWidget);
    QLabel *qDetail = mainWindowsWidget->findChild<QLabel *>(qWidget->objectName() + "_detailLabel");
    if(qDetail != nullptr){
        //qDebug() << "find " << qDetail->objectName();
        qDetail->setVisible(false);
    }

}

void Item::mouseMoveEventSlot(QMouseEvent* event)
{
    event->accept();

    if(enterQWidget(event->pos(), this->qWidget)){
        //qDebug() << "enter label" << this->id;
        showDetail(this->qWidget);
    } else {
        disableDetail(this->qWidget);
    }
}

void Item::mousePressEventSlot(QMouseEvent *event)
{

}

bool Item::enterQWidget(QPoint pp, QWidget* qWidget)
{
    int height = qWidget->height();
    int width = qWidget->width();

    QPoint widgetPos = getPosInMainWindow(qWidget);

    QPoint widgetMinPos = widgetPos;
    QPoint widgetMaxPos = widgetPos;

    widgetMaxPos.setX(widgetPos.x() + width);
    widgetMaxPos.setY(widgetPos.y() + height);

    if(pp.x() >= widgetMinPos.x() && pp.y() >= widgetMinPos.y()
            && pp.x() <= widgetMaxPos.x() && pp.y() <= widgetMaxPos.y()){
        return true;
    }
    else {
        return false;
    }
}

QWidget *Item::findMainWindowsWidget(QWidget *qWidget)
{
    QWidget* qParent = qWidget->parentWidget();
    int max = 10;
    while(QString::compare(qParent->objectName(), "MainWindow")){
        //qDebug() << "parentWidget: " << parentWidget->objectName() << "   posInMainWindow: " << posInMainWindow;
        qParent = qParent->parentWidget();

        if(max > 0){
            max--;
        }else{
            return nullptr;
        }
    }

    return qParent;
}

